package com.qhit.controller;


import com.qhit.pojo.Goods;
import com.qhit.pojo.Order;

import com.qhit.pojo.Tourist;
import com.qhit.service.GoodsService;
import com.qhit.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/list")
    public String list(Model model,@RequestParam String keywords,
                       @RequestParam(defaultValue = "1") Integer pageSize,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "1") Integer pageNo){
        HashMap hashMap = new HashMap<>();
        hashMap.put("keywords",keywords);
        hashMap.put("pageSize",pageSize);
        hashMap.put("page",page);
        hashMap.put("pageNo",pageNo);
        long total=orderService.getListCount(hashMap);
//        System.out.println(total);
        hashMap.put("totalPage",(total%pageSize==0?(total/pageSize):((total/pageSize)+1)));
        List<Map> list =  orderService.getList(hashMap);
        model.addAttribute("list",list);
        model.addAttribute("myParams",hashMap);
        return "order/list";
    }
//   @RequestMapping("/list2")
//    public String list2(Model model){
//        List<Goods> list =  goodsService.getList2();
//        model.addAttribute("list",list);
//        return "goods/list";
//    }
    @RequestMapping("/toAdd")
    public String toAdd (Model model){
        List<Goods> list = goodsService.getList(null);
        model.addAttribute("goodslist",list);
//        System.out.println(list);
        return "order/add";
    }
    @PostMapping("/add")
    public String add ( Order order , Model model){
        order.setOrderDate(new Date());
        order.setStatus(0);
        Goods goods=goodsService.getGoodsById(order.getGoodsId());
        order.setTotalMoney(goods.getMoney());
        int i = orderService.add(order);
        if(i>0){
            model.addAttribute("msg","添加成功");
        }else {
            model.addAttribute("msg","添加失败");
        }
        return "order/msg";
    }
    @RequestMapping("/toEdit/{id}")
    public String toEdit (@PathVariable("id") Integer id,Model model){
        Order order=orderService.getOrderById(id);
        List<Goods> list = goodsService.getList(null);
        model.addAttribute("goodslist",list);
        model.addAttribute("order",order);
        return "order/edit";
    }
    @RequestMapping("/delete/{id}")
    public String delete (@PathVariable("id") Integer id,Model model){
        int i=orderService.delete(id);
        if(i>0){
            model.addAttribute("msg","删除成功");
        }else {
            model.addAttribute("msg","删除成功");
        }
        return "order/msg";
    }
    @PostMapping("/edit")
    public String edit ( Order order , Model model){
        order.setOrderDate(new Date());
        order.setStatus(0);
        int i = orderService.edit(order);

        if(i>0){
            model.addAttribute("msg","修改成功");
        }else {
            model.addAttribute("msg","修改失败");
        }
        return "order/msg";
    }
    @RequestMapping("/editTourist/{id}")
    public String editTourist (@PathVariable("id") Integer id,Model model){
        Order order=orderService.getOrderById(id);
        model.addAttribute("order",order);
        return "order/touristList";
    }
    @PostMapping("/getTourist")
    @ResponseBody
    public List<Tourist> getTourist (@RequestParam(name = "orderid") Integer id){
//        System.out.println(id);
        List l =orderService.getTourist(id);
//        System.out.println(l.get(0));
        return l;
    }
    @PostMapping("/getTouristOut")
    @ResponseBody
    public List<Tourist> getTouristOut (@RequestParam(name = "orderid") Integer id){
//        System.out.println(id);
        List l =orderService.getTouristOut(id);
//        System.out.println(l.get(0));
        return l;
    }
    @PostMapping("/addTourist")
    @ResponseBody
    public String addTourist (@RequestParam(name = "orderid") Integer orderid,
                              @RequestParam(name = "touristid") String touristid){
        System.out.println(orderid+"...."+touristid);
        int l =orderService.addTourist(orderid,touristid);
        if(l>0){
            return "成功";
        }else {
            return "失败";
        }

    }
    @PostMapping("/delTourist")
    @ResponseBody
    public String delTourist (@RequestParam(name = "orderid") Integer orderid,
                              @RequestParam(name = "touristid") String touristid ){
        System.out.println(orderid+"...."+touristid);
        int l =orderService.delTourist(orderid,touristid);
        if(l>0){
            return "成功";
        }else {
            return "失败";
        }

    }
}
